package com.thread.threadpool;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author: may
 * @date: 2021年04月15日 20:14
 */
public class TestCompletableFuture {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        CompletableFuture<Double> tm = CompletableFuture.supplyAsync(() -> priceOfTM());
        CompletableFuture<Double> tb = CompletableFuture.supplyAsync(() -> priceOfTB());
        CompletableFuture<Double> jd = CompletableFuture.supplyAsync(() -> priceOfJD());
        CompletableFuture.allOf(tm, tb, jd).join();

        /*CompletableFuture.supplyAsync(() -> priceOfJD())
                .thenApply(String::valueOf)
                .thenApply(str -> "price" + str)
                .thenAccept(System.out::println);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        long end = System.currentTimeMillis();
        System.out.println("use CompletableFuture take time:" + (end - start));
    }

    public static Double priceOfTM() {
        delay();
        return 1.0;
    }

    public static Double priceOfTB() {
        delay();
        return 2.0;
    }

    public static Double priceOfJD() {
        delay();
        return 3.0;
    }

    private static void delay() {
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
